package cn.neusoft.luofa603.yuejian.Adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by P.Block on 2017/09/27.
 */

public abstract class DataBindingRecyclerAdapter<M,B extends ViewDataBinding> extends RecyclerView.Adapter {
    protected Context context;
    protected List<M> items  = new ArrayList<>();;

    public DataBindingRecyclerAdapter(Context context, List<M> data){
        this.context = context;
        this.items = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        B binding = DataBindingUtil.inflate(LayoutInflater.from(context),this.getLayoutResId(),parent,false);
        return new BaseBindingViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        B binding = DataBindingUtil.getBinding(holder.itemView);
        this.onBindItem(binding,this.items.get(position));
    }

    @Override
    public int getItemCount() {
        return this.items.size();
    }

    public class BaseBindingViewHolder extends RecyclerView.ViewHolder{

        public BaseBindingViewHolder(View itemView) {
            super(itemView);
        }

    }

    protected abstract @LayoutRes int getLayoutResId();

    protected abstract void onBindItem(B binding,M item);
}
